using System;
using System.IO;
using System.Reflection;
using System.Xml;
using XmlObjects.XmlDocumentMarkup;
using XmlObjects.XmlDocumentMarkup.XmlWriter;

namespace XmlObjects
{
	public class AttributePropertyReader : DocumentPropertyReader
	{
		private IXmlSimpleEntity simpleEntity;

		private string attributeName;
		public AttributePropertyReader()
		{
			
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="propertyInfo">PropertyInfo object representing the property on the object</param>
		public AttributePropertyReader(PropertyInfo propertyInfo) : base(propertyInfo)
		{
			XmlAttributeAttribute attribute = (XmlAttributeAttribute)propertyInfo.GetCustomAttributes(typeof(XmlAttributeAttribute), true)[0];
			if (attribute!=null)
				attributeName = attribute.Name;
			else
			{
//				XmlElementAttribute elementAttribute = propertyInfo.GetCustomAttributes();
			}
		}

		public override void WriteXml(XmlNode node, DocumentElement theObject, XmlDocument ownerDocument)
		{
			XmlAttribute theAttribute = null;
			foreach (XmlAttribute anAttribute in node.Attributes)
			{
				if (attributeName.Equals(anAttribute.Name))
				{
					theAttribute = anAttribute;
				}
			}

			if (null==theAttribute)
			{
				theAttribute = ownerDocument.CreateAttribute(attributeName);
				node.Attributes.Append(theAttribute);
			}
			theAttribute.Value = propertyInfo.GetValue(theObject, null).ToString();
		}

		public static bool IsMyType(PropertyInfo propertyInfo)
		{
			return (GetAttribute<XmlAttributeAttribute>(propertyInfo)!=null) && CanHandleDataType(propertyInfo);
		}

		protected static bool CanHandleDataType(PropertyInfo propertyInfo)
		{
			return (propertyInfo.PropertyType == typeof(string) 
				|| propertyInfo.PropertyType == typeof(int)
				|| propertyInfo.PropertyType == typeof(float)
				|| propertyInfo.PropertyType == typeof(decimal)
				|| propertyInfo.PropertyType == typeof(double)
				|| propertyInfo.PropertyType == typeof(DateTime)
				|| propertyInfo.PropertyType == typeof(FileInfo));			
		}

		public override void ReadXml(XmlNode node, object theObject)
		{
			string stringValue = node.Attributes[attributeName].Value;
			if (theObject != null) propertyInfo.SetValue(theObject, GetTheValue(stringValue), null);
		}

		protected object GetTheValue(string stringValue)
		{
			object theValue = null;

			if (propertyInfo.PropertyType == typeof(string))
				theValue = stringValue;
			else if (propertyInfo.PropertyType == typeof(int))
				theValue =  stringValue == null ? 0 : int.Parse(stringValue);
			else if (propertyInfo.PropertyType == typeof(float))
				theValue = stringValue == null ? 0 : float.Parse(stringValue);
			else if (propertyInfo.PropertyType == typeof(decimal))
				theValue = stringValue == null ? 0 : decimal.Parse(stringValue);
			else if (propertyInfo.PropertyType == typeof(double))
				theValue = stringValue == null ? 0 : double.Parse(stringValue);
			else if (propertyInfo.PropertyType == typeof(DateTime))
				theValue = stringValue == null ? DateTime.MinValue : DateTime.Parse(stringValue);
			else if (propertyInfo.PropertyType == typeof(FileInfo))
				theValue = stringValue == null ? null : new FileInfo(stringValue);
			return theValue;
		}
	}
}